<?php
/**
 * Note: *
 */
namespace App\Models\Game\Center;

use App\Traits\ModelTrait;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Facades\Storage;

class Apk extends Base
{
    use ModelTrait;

    public const STATUS_INIT = 0;// 初始化
    public const STATUS_MAKED = 1;// 已生成
    public const STATUS_UPLOAD = 2;// 已上传

    public $table = 'apks';

    public $fillable = [
        'appid',
        'source',
        'domain_group_name',
        'domain_platform',
        'base_apk',
        'name',
        'path',
        'status'
    ];

    protected $casts = [
        // 'domain' => 'array',
    ];

    protected $appends = [
        'download_url',
        'base_path',
        'full_path',
    ];

    /**
     * @return string
     */
    public function getBasePathAttribute(): string
    {
        return config('game.apk.basePath') . $this->base_apk;
    }

    /**
     * @return string
     */
    public function getFullPathAttribute(): string
    {
        return config('game.apk.outputPath') . $this->name;
    }

    /**
     * 下载地址
     * @return \Illuminate\Config\Repository|\Illuminate\Contracts\Foundation\Application|mixed|string
     */
    public function getDownloadUrlAttribute()
    {
        if (!$this->path) return "";
        if (app()->isLocal()) return Storage::disk('static')->url($this->path);
        return getCdnUrl($this->path);
    }

    /**
     * 关联 AppSource
     * @return BelongsTo
     */
    public function appSource()
    {
        return $this->belongsTo(AppSource::class, 'source', 'id');
    }
}
